/**
 * 
 */
package bedside.gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import bedside.VitalSign;

/**
 * @author Eric
 *
 */
public class SetAlarmDialog extends JDialog implements ActionListener {

	private static final String OK = "OK";
	private static final String CANCEL = "Cancel";
	private JTextField criticalLow;
	private JTextField low;
	private JTextField high;
	private JTextField criticalHigh;
	private final VitalSign vitalSign;

	public SetAlarmDialog( VitalSign vitalSign ) {
				
		this.vitalSign = vitalSign;
		this.setLayout( new GridLayout( 0, 1) );
		
		JPanel panel = new JPanel();
		JLabel label = new JLabel( "Critical high:" );
		panel.add( label );
		criticalHigh = new JTextField( 20 );
		panel.add( criticalHigh );
		this.add( panel );
		
		panel = new JPanel();
		label = new JLabel( "High:" );
		panel.add( label );
		high = new JTextField( 20 );
		panel.add( high );
		this.add( panel );
		
		panel = new JPanel();
		label = new JLabel( "Low:" );
		panel.add( label );
		low = new JTextField( 20 );
		panel.add( low );
		this.add( panel );
		
		panel = new JPanel();
		label = new JLabel( "Critical Low:" );
		panel.add( label );
		criticalLow = new JTextField( 20 );
		panel.add( criticalLow );
		this.add( panel );
		
		// Now create a panel with the OK and CANCEL buttons
		panel = new JPanel();
		JButton okButton = new JButton( OK );
		okButton.addActionListener( this );
		panel.add( okButton );
		
		JButton cancelButton = new JButton( CANCEL );
		cancelButton.addActionListener( this );
		panel.add( cancelButton );
		
		this.add( panel );
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String action = e.getActionCommand();
		if ( action.equals( OK ) ) {
			int critLowVal = Integer.parseInt( criticalLow.getText() );
			int lowVal = Integer.parseInt( low.getText() );
			int highVal = Integer.parseInt( high.getText() );
			int critHighVal = Integer.parseInt( criticalHigh.getText() );
			vitalSign.setLimits( critLowVal, lowVal, highVal, critHighVal );
			
			// Now dispose of the dialog
			this.dispose();
		} else if ( action.equals( CANCEL ) ) {
			// Just dispose of the dialog
			this.dispose();
		}
	}
}
